package cn.semyes.ylipfilter.controller.ipconfig;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.xml.resolver.helpers.PublicId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.semyes.ylipfilter.common.util.GetHtmlSourceUtil;
import cn.semyes.ylipfilter.common.util.R;


@RestController
@RequestMapping("/configure")
public class IpConfigureController {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private String htmlStr;
	private String url;
	private StringBuffer sb = null;
	
	@GetMapping("/index.do")
	public ModelAndView getIndex() {
		ModelAndView mav = new ModelAndView("index.html");
		return mav;
	}
	
	
	@GetMapping("/getHtmlSource.do")
	public R gethtmlsource(@RequestParam Map<String, Object> params) {
		url = params.get("url").toString();
		logger.info("需要检查是否已安装统计代码的域名："+url);
		sb = new StringBuffer(url);
		if (url.indexOf("www") == -1) {
			  
			sb.insert(0, "www.");
			url = sb.toString();
		}
		if (url.indexOf("http") == -1) {
			sb = new StringBuffer(url);  
			sb.insert(0, "http://");
		}
		htmlStr =GetHtmlSourceUtil.getHtmlSource(sb.toString());
		//return R.ok().put("data", htmlStr);
		if(htmlStr.indexOf("www.nisure.cn/piwikdatalistener/consultAnalyze.js")!=-1){  
			logger.info("此网站包含易数宝统计代码"); 
			return R.ok().put("data", 1);
		}else{ 
			logger.info("此网站不包含易数宝统计代码"); 
		} 
		return R.ok().put("data", -1);
		
	}
	
	@GetMapping("/getCheckUrl.do")
	public R getCheckUrl(HttpServletRequest request) {
		return R.ok().put("data", request.getSession().getAttribute("cur_domain").toString());
	}
	

}
